//
//  SelectedAuthodExpresserViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/10/27.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "SelectedAuthodExpresserViewController.h"
#import "ReceivedExpressViewController.h"
#import "ExpressReceiveViewController.h"
#import "AuthodExpresserViewController.h"
#import "SocketRequest.h"
#import "Singleton.h"
#import "ExpresserDBModel.h"

@interface SelectedAuthodExpresserViewController ()<SocketRequestDelegate,AuthodExpresserViewControllerDelegate> {
    SocketRequest *_socketRequest;
    ExpresserDBModel *_expresser;
}

@end

@implementation SelectedAuthodExpresserViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.expressSendType == ExpressSendTypeZiti) {
        self.title = @"接收自提件";
        [self rightBarButtonItemTitle:@"已接收" Method:@selector(pullReceive)];
    } else if (self.expressSendType == ExpressSendTypeVisit) {
        self.title = @"接收上门件";
        [self rightBarButtonItemTitle:@"已接收" Method:@selector(pullReceive)];
    }
}

- (void)pullReceive {
    if (self.expressSendType == ExpressSendTypeZiti) {
        [MobClick event:@"zitijian_link_yijieshou"];
    } else if (self.expressSendType == ExpressSendTypeVisit) {
        [MobClick event:@"shangmenjian_link_yijieshou"];
    }
    ReceivedExpressViewController *receivedEVC = [[ReceivedExpressViewController alloc] initWithNibName:@"ReceivedExpressViewController" bundle:nil];
    receivedEVC.expressSendType = self.expressSendType;
    [self.navigationController pushViewController:receivedEVC animated:YES];
}

//获取批次号
- (void)getBatchNoSocket {
    [ToastActivity makeActivity:@"加载中..." InView:self.view];;
    NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STS2"]];
    [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.websiteId] length:64];
    [_socketRequest addSocketData:mutableData SocketTag:2];
}

#pragma mark AuthodExpresserViewControllerDelegate
- (void)didFinishSelectedAuthodExpresser:(ExpresserDBModel *)expresser {
    _expresser = expresser;
    _expresserTextField.text = [NSString stringWithFormat:@"%@(%@)",expresser.expressCompanyName,expresser.expresserName];
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    [ToastActivity dismissInView:self.view];
    if ([code isEqualToString:SuccessSocket]) {
        if (self.expressSendType == ExpressSendTypeZiti) {
            [MobClick event:@"shangmenjian_btn_jieshou"];
        } else if (self.expressSendType == ExpressSendTypeVisit) {
            [MobClick event:@"zitijian_btn_jieshou"];
        }
        NSString *batchNo = [SocketMethod stringData:data];
        ExpressReceiveViewController *expressRVC = [[ExpressReceiveViewController alloc] initWithNibName:@"ExpressReceiveViewController" bundle:nil];
        expressRVC.expresser = _expresser;
        expressRVC.expressSendType = self.expressSendType;
        expressRVC.batchNo = batchNo;
        [self.navigationController pushViewController:expressRVC animated:YES];
    } else {
        [DSToast showText:msg InView:self.view];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [ToastActivity dismissInView:self.view];
    [DSToast showText:msg InView:self.view];
}

#pragma mark NibAction
- (IBAction)selectedAuthodExpresserAction:(id)sender {
    if (self.expressSendType == ExpressSendTypeZiti) {
        if ([Singleton shareInstance].userMessage.zitiIsSend) {
            [self selectedExpresser];
        } else {
            [DSToast showText:@"请引导快递员使用腾云管家APP进行投递" InView:self.view];
        }
    } else if (self.expressSendType == ExpressSendTypeVisit) {
        [self selectedExpresser];
    }
}

- (void)selectedExpresser {
    AuthodExpresserViewController *authodEVC = [[AuthodExpresserViewController alloc] initWithNibName:@"AuthodExpresserViewController" bundle:nil];
    authodEVC.delegate = self;
    [self.navigationController pushViewController:authodEVC animated:YES];
}

- (IBAction)startReceiveExpressAction:(id)sender {
    if (self.expressSendType == ExpressSendTypeZiti) {
        if ([Singleton shareInstance].userMessage.zitiIsSend) {
            [self startScanningExpresse];
        } else {
            [DSToast showText:@"请引导快递员使用腾云管家APP进行投递" InView:self.view];
        }
    } else if (self.expressSendType == ExpressSendTypeVisit) {
        [self startScanningExpresse];
    }
}

- (void)startScanningExpresse {
    if (_expresser == nil) {
        [DSToast showText:@"请选择快递员" InView:self.view];
    } else {
        [self getBatchNoSocket];
    }
}

@end
